﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;

namespace CommonLibrary
{
    /// <summary>
    /// 时序实体
    /// </summary>
    public class DeviceTimeLine
    {
        /// <summary>
        /// 工位
        /// </summary>
        [Description("工位")]
        [JsonProperty(PropertyName = "TimeLineCode")]
        public string TimeLineCode { get; set; } = "";
        /// <summary>
        /// 名称
        /// </summary>
        [Description("名称")]
        [JsonProperty(PropertyName = "TimeLineName")]
        public string TimeLineName { get; set; } = "";
        /// <summary>
        /// PLC地址
        /// </summary>
        [Description("PLC地址")]
        [JsonProperty(PropertyName = "TimeLinePLCAddress")]
        public string TimeLinePLCAddress { get; set; } = "";
        /// <summary>
        /// PLC读取的类型
        /// </summary>
        [JsonProperty(PropertyName = "TimeLineDataType")]
        public string TimeLineDataType { get; set; } = "float";
        /// <summary>
        /// 值
        /// </summary>
        [Description("值")]
        [JsonProperty(PropertyName = "TimeLineValue")]
        public string TimeLineValue { get; set; } = "";
        /// <summary>
        /// 是否正常数据
        /// </summary>
        /// <returns></returns>
        public bool IsValid()
        {
            return !string.IsNullOrWhiteSpace(this.TimeLineCode) && !string.IsNullOrWhiteSpace(this.TimeLineName) && !string.IsNullOrWhiteSpace(this.TimeLinePLCAddress);
        }
    }

    /// <summary>
    /// 时序配置
    /// </summary>
    public class TimeLine
    {
        /// <summary>
        /// 时序实体列表
        /// </summary>
        public List<DeviceTimeLine> DeviceTimeLines { get; set; } = new List<DeviceTimeLine>();
        /// <summary>
        /// 配置文件
        /// </summary>
        private static string CONFIG_FILE_PATH = Path.Combine(CommonHelper.COMMON_CONFIG_DIR, "DeviceTimeLine.json");
        /// <summary>
        /// 锁
        /// </summary>
        private static object LOCK_OBJ = new object();
        /// <summary>
        /// 单例对象
        /// </summary>
        private static TimeLine instance = null;
        /// <summary>
        /// 单例对象
        /// </summary>
        /// <param name="reload"></param>
        /// <returns></returns>
        public static TimeLine Instance(bool reload = false)
        {
            if (reload || instance == null)
            {
                instance = new TimeLine();
                try
                {
                    instance.DeviceTimeLines = CommonHelper.GetJsonFromFile(CONFIG_FILE_PATH, new List<DeviceTimeLine>());
                }
                catch (Exception ex)
                {
                    instance.DeviceTimeLines = new List<DeviceTimeLine>();
                    LogHelper.Error(ex);
                }
            }
            return instance;
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            lock (LOCK_OBJ)
            {
                CommonHelper.SaveToJsonFile(instance.DeviceTimeLines, CONFIG_FILE_PATH);
            }
        }
    }
    /// <summary>
    /// 时序帮助类
    /// </summary>
    public class TimeLineHelper
    {
        /// <summary>
        /// 导出时序数据到Excel文档
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool ExportToExcel(string filePath)
        {
            bool result = false;
            var timeLines = TimeLine.Instance().DeviceTimeLines;
            if (timeLines != null && timeLines.Count > 0 && APPInstance.PLC != null && APPInstance.PLC.IsConnected)
            {
                foreach (var timeLine in timeLines)
                {
                    if (!string.IsNullOrWhiteSpace(timeLine.TimeLinePLCAddress))
                    {
                        timeLine.TimeLineValue = APPInstance.PLC.Read(CommonHelper.GetEnumByName(timeLine.TimeLineDataType.ToUpper(), EDataType.SHORT), timeLine.TimeLinePLCAddress).ToString();
                    }
                }
                result = WriteExcel(filePath, timeLines);
            }
            return result;
        }
        /// <summary>
        /// 写入Excel
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="timeLines"></param>
        /// <returns></returns>
        private static bool WriteExcel(string filePath, List<DeviceTimeLine> timeLines)
        {
            bool result = false;
            try
            {
                result = CommonHelper.WriteExcel(filePath, timeLines, true, "时序数据", null, true, true, new string[] { "TimeLinePLCAddress" });
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }
            return result;
        }
    }
}
